package cn.yeziji.forum.dao.iquery;

import cn.org.atool.fluent.mybatis.base.crud.IQuery;
import cn.org.atool.fluent.mybatis.base.crud.JoinBuilder;
import cn.yeziji.forum.entity.ForumChatroomEntity;
import cn.yeziji.forum.entity.ForumUserEntity;
import cn.yeziji.forum.wrapper.ForumChatroomQuery;
import cn.yeziji.forum.wrapper.ForumUserQuery;

/**
 * 聊天室查询构造器
 *
 * @author gzkemays
 * @since 2022/3/28 20:44
 */
public class ChatroomQuery {
  /**
   * 根据聊天室名称获取聊天记录
   *
   * <p>根据 createTime 进行降序
   *
   * @param chatroom 聊天室房间名
   * @return 查询条件构造器
   */
  public static ForumChatroomQuery getRecordsByChatroomName(String chatroom) {
    return new ForumChatroomQuery()
        .selectAll()
        .where
        .delete()
        .isFalse()
        .and(q -> q.where.chatroom().eq(chatroom).end())
        .end()
        .orderBy
        .createTime()
        .desc()
        .end();
  }

  public static IQuery getRecordsAndUserMsgByChatroomName(String chatroom) {
    return JoinBuilder.from(
            new ForumChatroomQuery()
                .select
                .chatroom()
                .from()
                .to()
                .context()
                .createTime("publishTime")
                .end()
                .where
                .delete()
                .isFalse()
                .and(q -> q.where.chatroom().eq(chatroom).end())
                .end()
                .orderBy
                .createTime()
                .desc()
                .end())
        .leftJoin(new ForumUserQuery().select.avatar().end())
        .onEq(ForumChatroomEntity::getFrom, ForumUserEntity::getNickname)
        .endJoin()
        .build();
  }

  /**
   * 根据 Nickname 获取聊天室数据
   *
   * @param nickname 用户昵称
   * @return 查询条件构造器
   */
  public static ForumChatroomQuery getChatroomByNickname(String nickname) {
    return new ForumChatroomQuery()
        .selectAll()
        .where
        .to()
        .eq(nickname)
        .or
        .from()
        .eq(nickname)
        .end();
  }

  /**
   * 根据 nickname 查找聊天室的分组数据
   *
   * @param nickname 用户昵称
   */
  public static ForumChatroomQuery getChatroomByNicknameGroupByChatroom(String nickname) {
    return new ForumChatroomQuery()
        .selectAll()
        .where
        .to()
        .eq(nickname)
        .or
        .from()
        .eq(nickname)
        .end()
        .groupBy
        .chatroom()
        .end();
  }

  /**
   * 根据聊天室获取最后一条信息
   *
   * @param chatroom 聊天室
   */
  public static ForumChatroomQuery getLastMsgByNickname(String chatroom) {
    return new ForumChatroomQuery()
        .select
        .context()
        .createTime()
        .end()
        .where
        .chatroom()
        .eq(chatroom)
        .end()
        .orderBy
        .createTime()
        .desc()
        .end()
        .limit(0, 1);
  }

  public static ForumChatroomQuery getUserStatisticQuery(String nickname) {
    return new ForumChatroomQuery()
        .selectAll()
        .where
        .to()
        .eq(nickname)
        .and
        .updateTime()
        .isNull()
        .end();
  }
}
